<template>
  <a-modal
    :title="this.title"
    :visible="this.visible"
    :width="620"
    @ok="ok"
    @cancel="cancel"
  >
    <a-form :form="form" layout="horizontal">

      <a-form-item
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        label="组织机构名称"
      >
          <a-input v-decorator="['name', {   initialValue: this.ogName,rules: [{ required: true, message: '请输入组织机构名称!' },{
            pattern: /^[a-zA-Z0-9\u4e00-\u9fa5]{1,30}$/,
            message: '组织机构名为1至30位汉字、字母、数字' }] }]" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
  import aModal from "@/common/modal"
  import { ADDDEPARTMENT,UPDATEDEPARTMENT } from '@/../static/js/apis.js';
  export default {
    inject:['reload'],                                 //注入App里的reload方法
    name: "organizeModal",
    props: ['title', 'visible','value','type','ogName'],
    components: {
      aModal
    },
    data() {
      return {
        form: this.$form.createForm(this),
        labelCol: {span: 5},
        wrapperCol: {span: 16},
      }
    },
    methods: {
      ok (e) {
        const self = this
        e.preventDefault()
        self.form.validateFields((err, values) => {
          if (!err) {
            if(self.type=='下级菜单'){
                values.organizationid = this.value
                console.log('Received values of form: ', values)
                ADDDEPARTMENT(values, function (res) {
                  if (res.code=='200') {
                    self.$message.success(res.message)
                    self.$emit('ok', e)
                    self.reload()
                  } else self.$message.error(res.message)
                })
            }else if(self.type=='一级菜单'){
                values.organizationid =1
               ADDDEPARTMENT(values, function (res) {
                  if (res.code=='200') {
                    self.$message.success(res.message)
                    self.$emit('ok', e)
                    self.reload()
                  } else self.$message.error(res.message)
                })
            }else if(self.type=='编辑菜单'){
                values.id = this.value
               UPDATEDEPARTMENT(values, function (res) {
                  if (res.code=='200') {
                    self.$message.success(res.message)
                    self.$emit('ok', e)
                    self.reload()
                  } else self.$message.error(res.message)
                })
            }

          } else {
            self.$message.error('请按要求填写组织机构信息！')
          }
        })
      },
      /*关闭modal*/
      cancel(e) {
        this.form.resetFields();
        this.$emit("cancel", e)
      },

    }
  }
</script>

<style scoped>

</style>
